package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

@SuppressWarnings("deprecation")
public class ShopDetailData {
	static FinalDb db;

	/**
	 * 获取店铺详情信息
	 * 
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getShopDetails(String shopId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/shopDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("shop_id", shopId);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			Log.e("strJson", strJson);

			resMap.put("code", jsonObject.getString("code"));
			resMap.put("message", jsonObject.getString("message"));

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				resMap.put("id", jsondata.getString("id"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("status", jsondata.getString("status"));
				resMap.put("shop_pic", jsondata.getString("shop_pic"));
				resMap.put("shop_desc", jsondata.getString("shop_desc"));
				resMap.put("shop_name", jsondata.getString("shop_name"));
				resMap.put("cate_name", jsondata.getString("cate_name"));
				resMap.put("city_name", jsondata.getString("city_name"));

				resMap.put("good_comment_rate",
						jsondata.getString("good_comment_rate"));
				resMap.put("avg_score", jsondata.getString("avg_score"));
				resMap.put("total_service", jsondata.getString("total_service"));
				resMap.put("isEnterprise", jsondata.getString("isEnterprise"));
				resMap.put("bank", jsondata.getString("bank"));
				resMap.put("email", jsondata.getString("email"));
				resMap.put("realname", jsondata.getString("realname"));

				resMap.put("goods_num", jsondata.getString("goods_num"));
				resMap.put("service_num", jsondata.getString("service_num"));
				resMap.put("success_case_num",
						jsondata.getString("success_case_num"));
				resMap.put("comment_num", jsondata.getString("comment_num"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	// 没有店铺人的详情
	public static Map<String, String> getuserDetail(String uid, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/userDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("uid", uid);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			resMap.put("code", jsonObject.getString("code"));
			resMap.put("message", jsonObject.getString("message"));

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				resMap.put("id", jsondata.getString("id"));
				resMap.put("name", jsondata.getString("name"));
				resMap.put("avatar", jsondata.getString("avatar"));
				resMap.put("introduce", jsondata.getString("introduce"));
				resMap.put("cate_name", jsondata.getString("cate_name"));
				resMap.put("service_num", jsondata.getString("service_num"));
				resMap.put("city_name", jsondata.getString("city_name"));

				resMap.put("good_comment", jsondata.getString("good_comment"));
				resMap.put("total_comment", jsondata.getString("total_comment"));
				resMap.put("good_comment_rate",
						jsondata.getString("good_comment_rate"));
				resMap.put("avg_score", jsondata.getString("avg_score"));

				resMap.put("isEnterprise", jsondata.getString("isEnterprise"));
				resMap.put("bank", jsondata.getString("bank"));
				resMap.put("alipay", jsondata.getString("alipay"));
				resMap.put("realname", jsondata.getString("realname"));
				resMap.put("email", jsondata.getString("email"));

				resMap.put("comment_num", jsondata.getString("comment_num"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	public static ArrayList<HashMap<String, String>> getShopCate(String string) {
		ArrayList<HashMap<String, String>> cateList = new ArrayList<HashMap<String, String>>();
		try {
			JSONArray jArray = new JSONArray(string);

			if (jArray.length() > 0) {
				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("cate", jArray.getString(i));

					cateList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cateList;
	}

	/**
	 * 店铺详情的全部作品
	 * 
	 * @param strShopId
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getShopWork(
			String strShopId, int page, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/workList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("shop_id", strShopId);
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");
			JSONObject jsondata = new JSONObject(resData);
			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("unit", itemJsonObject.getString("unit"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("cover", itemJsonObject.getString("cover"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("percent", itemJsonObject.getString("percent"));
						taskList.add(map);
					}
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 店铺的全部服务
	 * 
	 * @param strShopId
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getShopService(
			String strShopId, int page, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/serviceList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();

		params.put("page", "" + page);
		params.put("shop_id", "" + strShopId);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("unit", itemJsonObject.getString("unit"));
						map.put("cover", itemJsonObject.getString("cover"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("city_name", "");
						map.put("cate_name",
								itemJsonObject.getString("cate_name"));
						map.put("percent", itemJsonObject.getString("percent"));
						taskList.add(map);
					}
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 店铺的全部案例
	 * 
	 * @param strShopId
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getShopCase(
			String strShopId, int page, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/successList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();

		params.put("page", "" + page);
		params.put("shop_id", "" + strShopId);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			JSONArray jsonArray = jsondata.getJSONArray("data");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("pic", itemJsonObject.getString("pic"));

						taskList.add(map);
					}
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	public static String collectStatus(String shopId, Context context) {
		String strResult = "0";

		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/collectStatus?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("shop_id", "" + shopId);

		if (users.size() > 0) {
			params.put("token", "" + users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			JSONObject jsondata = new JSONObject(jsonObject.getString("data"));
			strResult = jsondata.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strResult;
	}

	/**
	 * 收藏店铺
	 * 
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static int addShopFocus(String shopId, Context context) {
		int intResult = 0;

		String urlString = ConfigInc.getServiceAdress(context) + "shop/collect";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("shop_id", shopId));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			intResult = Integer.valueOf(jsonObject.getString("code"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return intResult;
	}

	/**
	 * 删除收藏店铺
	 * 
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static int delShopFocus(String shopId, Context context) {
		int intResult = 0;

		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/cancelCollect";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("shop_id", shopId));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			intResult = Integer.valueOf(jsonObject.getString("code"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return intResult;
	}

	/**
	 * 服务商评价
	 * 
	 * @param strId
	 * @param strType
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> taskCommentList(
			String strId, String strType, Context context) {
		ArrayList<HashMap<String, String>> taskList = new ArrayList<HashMap<String, String>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/taskCommentList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("uid", strId);
		params.put("type", strType);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			JSONObject jsondata = jsonObject.getJSONObject("data");
			JSONArray jArray = jsondata.getJSONArray("data");

			if (jArray.length() > 0) {
				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject itemJsonObject = jArray.optJSONObject(i);

					map.put("name", itemJsonObject.getString("name"));
					map.put("avatar", itemJsonObject.getString("avatar"));
					map.put("avg_score", itemJsonObject.getString("avg_score"));
					map.put("comment_desc", itemJsonObject.getString("comment"));
					map.put("created_at",
							itemJsonObject.getString("created_at"));
					map.put("desc", itemJsonObject.getString("desc"));

					taskList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	public static Map<String, String> getShopEval(String shopId, int page,
			String strType, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/commentList?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("shop_id", shopId);
		params.put("page", "" + page);
		params.put("type", strType);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			resMap.put("code", jsonObject.getString("code"));
			resMap.put("message", jsonObject.getString("message"));

			JSONObject jsondata = new JSONObject(jsonObject.getString("data"));
			resMap.put("all_comment", jsondata.getString("all_comment"));
			resMap.put("good_comment", jsondata.getString("good_comment"));
			resMap.put("mid_comment", jsondata.getString("mid_comment"));
			resMap.put("bad_comment", jsondata.getString("bad_comment"));

			resMap.put("attitude_score", jsondata.getString("attitude_score"));
			resMap.put("speed_score", jsondata.getString("speed_score"));
			resMap.put("quality_score", jsondata.getString("quality_score"));
			resMap.put("avg_score", jsondata.getString("avg_score"));

			resMap.put("data", jsondata.getString("data"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	public static ArrayList<HashMap<String, String>> getShopEvalList(
			String string) {
		ArrayList<HashMap<String, String>> taskList = new ArrayList<HashMap<String, String>>();

		try {
			Log.e("string", "" + string);
			JSONArray jArray = new JSONArray(string);

			if (jArray.length() > 0) {
				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject itemJsonObject = jArray.optJSONObject(i);

					map.put("name", itemJsonObject.getString("name"));
					map.put("avatar", itemJsonObject.getString("avatar"));
					map.put("avg_score", itemJsonObject.getString("avg_score"));
					map.put("comment_desc",
							itemJsonObject.getString("comment_desc"));
					map.put("created_at",
							itemJsonObject.getString("created_at"));
					map.put("desc", itemJsonObject.getString("desc"));

					taskList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

}
